/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package atmosphere;

import com.jme3.app.Application;
import com.jme3.app.state.AppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.audio.AudioNode;
import com.jme3.renderer.RenderManager;

/**
 *
 * @author jiyarza
 */
public class JukeboxState implements AppState {

    // Music can't hurt, usually
    private AudioNode musicNode;
    private String[] jukebox = {
        "Sounds/EarthChant.ogg",
        "Sounds/SpaceDrums.ogg"
    };
    private int songIndex = 0;

    public JukeboxState() {
    }

    public void initialize(AppStateManager stateManager, Application app) {
        musicNode = new AudioNode(app.getAssetManager(), jukebox[songIndex++] , true);
        musicNode.setDirectional(false);
        musicNode.setLooping(false);
        musicNode.setVolume(0.0f);
        app.getAudioRenderer().playSource(musicNode);
    }

    public boolean isInitialized() {
        return musicNode != null;
    }

    public void setActive(boolean active) {        
    }

    public boolean isActive() {
        return true;
    }

    public void stateAttached(AppStateManager stateManager) {
    }

    public void stateDetached(AppStateManager stateManager) {
    }

    public void update(float tpf) {
        float v = musicNode.getVolume();
        if (v < 0.6f) {
            musicNode.setVolume(v + 0.001f);
        }
        if (musicNode.getStatus() == AudioNode.Status.Stopped) {
            musicNode.setVolume(0.0f);
            musicNode = new AudioNode(Prototype.getInstance().getAssetManager(), jukebox[songIndex++%jukebox.length], true);
            Prototype.getInstance().getAudioRenderer().playSource(musicNode);
        }
    }

    public void render(RenderManager rm) {
    }

    public void postRender() {
    }

    public void cleanup() {
        musicNode.setVolume(0.0f);
        musicNode = null;
    }

    public void setEnabled(boolean active) {        
    }

    public boolean isEnabled() {
        return true;
    }

}
